const app = getApp();
const utils = require('../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    CurrentPage: 1,
    loadingMore: 1,
    version: 5, //供货商列表版本： 5 云点单, 6 云代货
    staffList: [],
    createUid: 0, //带货人ID
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) { 
    if(!options.createUid){
      util.showModal('缺少商家id'); return;
    }
    this.setData({
      createUid: options.createUid,
      version: options.version
    })  
    this.getSupplyList();
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function (e) {
    this.loadMore();
  },
  //供货商列表
  getSupplyList: function () {    
    let that = this;
    utils.getLocation((location)=>{
      wx.request({
        url: app.globalData.host + '/api/supply/getlist?joinState=2&saleUid='+ that.data.createUid + '&version=' + that.data.version +'&latitude=' + location.lat + '&longitude=' + location.lng + '&page=' + that.data.CurrentPage,
        success: function (res) {      
          if (res.data.Code != 0) {     
            utils.showModal(res.data.Message); return;    
          }        
          var staffList = res.data.Data.Items;
          for (let i = 0; i < staffList.length; i++) {
            staffList[i].Avater = utils.formatAvater(staffList[i].Avater, 'https://static.711688.net.cn/upload/card/source/');
            staffList[i].ShowDiscount = staffList[i].Discount / 10;
            staffList[i].Distance = parseFloat(staffList[i].Distance).toFixed(2);
            staffList[i].Distances = parseFloat(staffList[i].Distance / 1000).toFixed(2);
          }
          if (that.data.CurrentPage != 1) {
            staffList =  that.data.staffList.concat(staffList);
          } 
          that.setData({
            staffList: staffList,
            loadingMore: res.data.Data.TotalPages
          })  
        }
      })
    });    
  },
   // 加载更多
   loadMore: function () {
    let that = this;
    if (that.data.loadingMore > 1 && that.data.CurrentPage < that.data.loadingMore) {
      let page = that.data.CurrentPage + 1
      that.setData({
        CurrentPage: page
      })
      that.getSupplyList();
    } 
  },
  //设置供货状态
  addSupply: function(e){
    let that = this;
    let uid = e.currentTarget.dataset.uid;
    wx.request({
      url: app.globalData.host + '/api/supply/AddSupply?saleUid='+ app.globalData.userInfo.Uid + '&uid=' + uid + '&version=' + that.data.version,
      success: function (res) {      
        if (res.data.Code != 0) {      
          utils.showModal(res.data.Message); return;      
        }
        utils.showToast('添加成功');
        setTimeout(() => {
          wx.redirectTo({
            url: '/merchant/supply/list/index?createUid=' + that.data.createUid + '&version=' + that.data.version ,
          });  
        }, 500);          
      }
    })
  }
})